#include "Coordinates.h"
#include "Point2D.h"
namespace HW2{

	Coordinates::Coordinates(double x, double y, double z, double h)
	{
		data.resize(4);
		X() = x;
		Y() = y;
		Z() = z;
		H() = h;
	}

	void Coordinates::normalize()
	{
		if(H() == 1)
			return;
		X() /= H();
		Y() /= H();
		Z() /= H();
		H() = 1;
	}

	Coordinates Coordinates::operator*(Matrix &m)
	{
		Coordinates v(0,0,0,0);
		for(int i = 1; i <= 4; i++)
		{
			for(int j = 1; j <= 4; j++)
			{
				v.get(i) += get(j)*m(j,i); 
			}
		}
		v.normalize();
		return v;
	}

	Coordinates& Coordinates::operator*=(Matrix &m)
	{
		Coordinates v = (*this)*m;
		return (*this = v);

	}

	Point2D Coordinates::toPoint2D()
	{ 
		return Point2D(X(), Y()); 
	}


	static double interpolate(double start, double end, double ratio)
	{
		return start + (end - start)* ratio;
	}

	Coordinates Coordinates::Interpolate(Coordinates &start, Coordinates &end,double ratio)
	{ 
		double x = interpolate(start.X(), end.X(), ratio);
		double y = interpolate(start.Y(), end.Y(), ratio);
		double z = interpolate(start.Z(), end.Z(), ratio);

		return Coordinates(x,y,z);
	}

}